package com.hunan.api.catv.common.rest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.http.client.BufferingClientHttpRequestFactory;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.http.client.OkHttp3ClientHttpRequestFactory;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.ResponseErrorHandler;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.LinkedList;
import java.util.List;


@Configuration
public class RestTemplateBeanConfig {

    protected Logger log = LoggerFactory.getLogger(getClass());


    @Bean
    public BufferingClientHttpRequestFactory dingHttpClientFactory() {
        OkHttp3ClientHttpRequestFactory httpRequestFactory = new OkHttp3ClientHttpRequestFactory();
        httpRequestFactory.setConnectTimeout(60000);
        httpRequestFactory.setReadTimeout(60000);
        return new BufferingClientHttpRequestFactory(httpRequestFactory);
    }

    @Primary
    @Bean("baseRestTemplate")
    public RestTemplate dingRestTemplate() {
        RestTemplate restTemplate = new RestTemplate(dingHttpClientFactory());
        List<ClientHttpRequestInterceptor> interceptors = new LinkedList<ClientHttpRequestInterceptor>();
        interceptors.add(new RestTamplateLogInterceptor());
        restTemplate.setInterceptors(interceptors);
        restTemplate.setErrorHandler(new ResponseErrorHandler() {
            @Override
            public boolean hasError(ClientHttpResponse response) throws IOException {
                if (response.getStatusCode().is4xxClientError()) {
                    return true;
                }
                if (response.getStatusCode().is5xxServerError()) {
                    throw new RestClientException("system error");
                }
                return false;
            }

            @Override
            public void handleError(ClientHttpResponse response) throws IOException {
                log.error("[rest Error: {} {} {}]", response.getStatusText(), response.getStatusCode(), response.getBody());
            }
        });
        StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter(Charset.defaultCharset());
        stringHttpMessageConverter.setWriteAcceptCharset(false);
        List<HttpMessageConverter<?>> list = restTemplate.getMessageConverters();
        list.add(stringHttpMessageConverter);
        restTemplate.setMessageConverters(list);
        StringHttpMessageConverter t = new StringHttpMessageConverter();
        t.setWriteAcceptCharset(false);
        restTemplate.getMessageConverters().add(0, t);
        return restTemplate;
    }
}